using System.Windows.Controls;
using aichat.ViewModels;

namespace aichat.Views
{
    public partial class ChatView : UserControl
    {
        public ChatView()
        {
            InitializeComponent();
            this.Loaded += ChatView_Loaded;
        }

        private void ChatView_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (DataContext is ChatViewModel viewModel)
            {
                viewModel.ScrollToBottomRequested += ViewModel_ScrollToBottomRequested;
            }
        }

        private void ViewModel_ScrollToBottomRequested(object? sender, System.EventArgs e)
        {
            // 延迟执行，确保UI更新完成后再滚动
            Dispatcher.BeginInvoke(() =>
            {
                if (chatScrollViewer != null)
                {
                    chatScrollViewer.ScrollToBottom();
                }
            });
        }
    }
} 